
package com.ami.kvm.jviewer.videorecord;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.common.ISOCFrameHdr;
import com.ami.kvm.jviewer.common.ISOCKvmClient;
import com.ami.kvm.jviewer.common.ISOCManager;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import com.ami.kvm.jviewer.gui.RecorderToolBar;
import com.ami.kvm.jviewer.gui.WindowFrame;
import com.ami.kvm.jviewer.jvvideo.JVVideo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.sql.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;

public class DisplayVideoDataTask extends TimerTask {
	public static boolean run = true;
	private ISOCFrameHdr m_frameHdr;
	private Date startTime;
	private static FileInputStream fc_read;
	private static int videoDuration = 0;
	public static Object playLock = new Object();
	public static boolean isPaused = false;
	private static int counter = 0;

	public static Timer timer;
	private static final int VIDEO_LENGTH_TAG_SIZE = 4;
	private static final int RESERVED_BYTES_VIDEO_LENGTH = 20;

	public DisplayVideoDataTask(byte[] paramArrayOfByte) {
		this.m_frameHdr = JViewerApp.getSoc_manager().getSOCFramehdr();

		if (JViewer.isdownloadapp()) {
			JViewerApp.getInstance().getVideorecordapp().showInformationDialog(LocaleStrings.getString("Y_1_DVDT"));
		}
	}

	public void run() {
		byte[] arrayOfByte1 = new byte[4];
		long l1 = 0L;
		long l2 = 0L;
		byte[] arrayOfByte2 = new byte[1];
		byte[] arrayOfByte3 = new byte[this.m_frameHdr.getFrameHeadersize()];
		byte[] arrayOfByte4 = new byte[3];
		int i = 0;
		run = true;
		long l3 = 0L;
		int j = JViewerApp.getInstance().getVideorecordapp().getFileIndex();
		byte[] arrayOfByte5 = new byte[20];

		try {
			fc_read = new FileInputStream(JViewerApp.getInstance().getVideorecordapp().getVideotmpstorepath()[j]);

			try {
				fc_read.read(arrayOfByte5, 0, arrayOfByte5.length);
				if (new String(arrayOfByte5, 0, 4).equals("len=") == true) {
					setVideoDuration((int) Double.parseDouble(new String(arrayOfByte5, 4, arrayOfByte5.length - 4)));

				} else {

					fc_read.close();
					fc_read = new FileInputStream(
							JViewerApp.getInstance().getVideorecordapp().getVideotmpstorepath()[j]);
				}
			} catch (IOException localIOException1) {
				Debug.out.println(localIOException1);
			}
		} catch (FileNotFoundException localFileNotFoundException) {
			Debug.out.println(localFileNotFoundException);
		}

		this.startTime = new Date(System.currentTimeMillis() / 1000L);
		Object localObject1;
		if ((JViewer.isplayerapp()) && (videoDuration != 0)) {
			localObject1 = new TimerTask() {

				public void run() {
				}

			};
			timer = new Timer();
			timer.scheduleAtFixedRate((TimerTask) localObject1, 0L, 100L);
			RecorderToolBar.updateComponents(videoDuration);
		}
		while (run) {

			localObject1 = ByteBuffer.wrap(arrayOfByte3);
			((ByteBuffer) localObject1).clear();
			try {
				i = fc_read.read(arrayOfByte1);
				if (i <= 0) {
					run = false;
					if (JViewer.isplayerapp())
						onenablemenu();
					cancel();
				} else {
					ByteBuffer localByteBuffer1 = ByteBuffer.wrap(arrayOfByte1);
					localByteBuffer1.order(ByteOrder.LITTLE_ENDIAN);
					l2 = localByteBuffer1.getInt();

					if (l1 == 0L) {
						l1 = l2;
					}

					i = fc_read.read(arrayOfByte2);
					if (i <= 0) {
						run = false;
						if (JViewer.isplayerapp())
							onenablemenu();
						cancel();

					} else {
						i = fc_read.read(arrayOfByte4);

						if ((arrayOfByte2[0] == 85) || (arrayOfByte2[0] == -86)) {
							l3 = l2 - l1;
							if (l3 > 0L) {
								try {
									Thread.sleep(l3 * 1000L);
								} catch (InterruptedException localInterruptedException1) {
									Debug.out.println(localInterruptedException1);
								}
							}
							if (arrayOfByte2[0] != -86) {
							}
						} else {
							if (arrayOfByte2[0] == 170) {
								continue;
							}

							if (arrayOfByte2[0] == 102) {
								l3 = l2 - l1;
								if (l3 > 0L) {
									try {
										Thread.sleep(l3 * 1000L);
									} catch (InterruptedException localInterruptedException2) {
										Debug.out.println(localInterruptedException2);
									}
								}
								JViewerApp.getInstance().getVidClnt().onBlankScreen();
								continue;
							}
							if (arrayOfByte2[0] == 119) {

								JViewerApp.getInstance().getSockvmclient().OnreadSOCVideoRecordData(fc_read);
								continue;
							}
						}
						l1 = l2;

						i = fc_read.read(arrayOfByte3);
						if (i <= 0) {
							run = false;
							if (JViewer.isplayerapp())
								onenablemenu();
							cancel();
						} else {
							((ByteBuffer) localObject1).position(this.m_frameHdr.getFrameHeadersize());
							this.m_frameHdr.setHeader((ByteBuffer) localObject1);
							((ByteBuffer) localObject1).position(0);

							if (this.m_frameHdr.getFrameSize() > 4608000) {
								run = false;
								if (JViewer.isplayerapp())
									onenablemenu();
								cancel();
								break;
							}
							byte[] arrayOfByte6 = new byte[this.m_frameHdr.getFrameSize()];

							i = fc_read.read(arrayOfByte6);

							if ((i < 0) || (i < this.m_frameHdr.getFrameSize())) {
								run = false;
								if (JViewer.isplayerapp())
									onenablemenu();
								cancel();
							} else {
								ByteBuffer localByteBuffer2 = ByteBuffer.allocate(
										this.m_frameHdr.getFrameSize() + this.m_frameHdr.getFrameHeadersize());

								localByteBuffer2.order(ByteOrder.LITTLE_ENDIAN);
								localByteBuffer2.put(arrayOfByte3, 0, this.m_frameHdr.getFrameHeadersize());
								localByteBuffer2.put(arrayOfByte6);

								localByteBuffer2.position(
										this.m_frameHdr.getFrameSize() + this.m_frameHdr.getFrameHeadersize());
								synchronized (getPlayLock()) {
									if (isPaused() == true) {
										playLock.wait();
									}
								}
								JViewerApp.getInstance().getVidClnt().onNewFrame(localByteBuffer2);
							}
						}
					}
				}
			} catch (Exception localException) {
				Debug.out.println(localException);
			}
		}

		if (JViewer.isdownloadapp()) {
			calculateDuration();
		}
		try {
			fc_read.close();
		} catch (IOException localIOException2) {
			Debug.out.println(localIOException2);
		}
	}

	private void onenablemenu() {
		JViewerApp.getInstance().initilizeJVVideo();
		RecorderToolBar localRecorderToolBar = (RecorderToolBar) JViewerApp.getInstance().getM_wndFrame().getToolbar();
		localRecorderToolBar.replayButton.setIcon(new ImageIcon(JViewer.class.getResource("res/play.png")));
		localRecorderToolBar.replayButton.setToolTipText(LocaleStrings.getString("R_1_RT"));
		localRecorderToolBar.replayButton.setActionCommand("");
	}

	public void calculateDuration() {
		long l = new Date(System.currentTimeMillis() / 1000L).getTime() - this.startTime.getTime();
		JViewerApp.getInstance().getVideorecordapp().setDuration(l);
	}

	public static FileInputStream getFc_read() {
		return fc_read;
	}

	public static Object getPlayLock() {
		return playLock;
	}

	public static void pause() {
		timer.cancel();
	}

	public static void resume() {
		if ((JViewer.isplayerapp()) && (videoDuration != 0)) {
			TimerTask local2 = new TimerTask() {

				public void run() {
				}

			};
			timer = new Timer();
			timer.scheduleAtFixedRate(local2, 0L, 100L);
		}
	}

	public static void update() {
		if (counter <= videoDuration * 10) {
			RecorderToolBar.getSlider().setValue(++counter);
			RecorderToolBar.getLabel()
					.setText(Integer.toString(counter / 10) + " / " + Integer.toString(videoDuration));
		} else {
			timer.cancel();
		}
	}

	public static int getVideoDuration() {
		return videoDuration;
	}

	public static void setVideoDuration(int paramInt) {
		videoDuration = paramInt;
	}

	public static int getCounter() {
		return counter;
	}

	public static void setCounter(int paramInt) {
		counter = paramInt;
	}

	public static boolean isPaused() {
		return isPaused;
	}

	public static void setPaused(boolean paramBoolean) {
		isPaused = paramBoolean;
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\videorecord\
 * DisplayVideoDataTask.class Java compiler version: 5 (49.0) JD-Core Version:
 * 0.7.1
 */